package com.jingdong.utils;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.Reader;

public class MyBatisUtils {
    public static SqlSessionFactory getSqlSessionFactory() throws  Exception{
        String source="mybatis-config.xml";
        Reader reader= org.apache.ibatis.io.Resources.getResourceAsReader(source);
        SqlSessionFactoryBuilder sqlSessionFactoryBuilder=new SqlSessionFactoryBuilder();
        SqlSessionFactory sqlSessionFactory=sqlSessionFactoryBuilder.build(reader);
        return sqlSessionFactory;
    }
    public  static SqlSession getSqlSession() throws Exception{
        SqlSessionFactory sqlSessionFactory=getSqlSessionFactory();
        return sqlSessionFactory.openSession();
    }
}
